package com.wuster.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class
Result<T> implements Serializable {

    private Integer code; //编码：200成功，500为失败。必须
    private String msg; //错误信息。非必须
    private T data; //数据，非必须

//    public static <T> Result<T> success() {
//        Result<T> result = new Result<T>();
//        result.code = 200;
//        return result;
//    }
//
//    public static <T> Result<T> success(T object) {
//        Result<T> result = new Result<T>();
//        result.data = object;
//        result.code = 200;
//        return result;
//    }

//    public static <T> Result<T> error(String msg) {
//        Result result = new Result();
//        result.msg = msg;
//        result.code = 500;
//        return result;
//    }

    /**
     * zhoujuan添加
     */
    public Result() {
    }

    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(data);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMessage());
        return result;
    }
    public static <T> Result<T> success(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> success() {
        return Result.success(null);
    }

    public static <T> Result<T> error() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> error(String msg) {//原来的处理方法，建议修改
        Result result = new Result();
        result.msg = msg;
        result.code = 500;
        return result;
    }

    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

}
